package com.topscomm.mis.cbb.pm.controller;

import com.topscomm.basic.exception.MessageException;
import com.topscomm.basic.exception.UnauthorizedException;
import com.topscomm.cbo.pojo.CboSystemErrorLogEntity;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;
import com.topscomm.tap.common.DatacodeCache;
import com.topscomm.tap.common.SessionUserBean;
import com.topscomm.tap.constant.TapHttpErrorCode;
import com.topscomm.tap.constant.TapHttpStatus;
import com.topscomm.tap.threadlocal.ThreadLocalContext;
import com.topscomm.tap.util.ErrorLogUtil;

import javax.annotation.Resource;
import java.util.Map;

public class CbbPmController {
    @Resource(name = "datacodeCache")
    protected DatacodeCache datacodeCache;

    public SessionUserBean getUserBean() {
        return ThreadLocalContext.sessionUserBeanThreadLocal.get();
    }


    /**
     * @Description:    处理服务调用异常
     * @Author:         wuyuyang
     * @Return:         com.topscomm.pub.vo.ResponseResult<java.lang.Object>
     * @exception:
     * @Date:           28/6/2023 上午9:53
     * @Version:        1.0
     **/
    protected ResponseResult<Object> handleControllerException(Exception ex) {
        if (ex instanceof MessageException) {
            return handleControllerMessage(ex.getMessage());
        }
        CboSystemErrorLogEntity logEntity = ErrorLogUtil.writeLog(ex);
        // UnauthorizedException异常处理
        if (ex instanceof UnauthorizedException) {
            return ResponseResult.error(((UnauthorizedException) ex).getStatus(), TapHttpErrorCode.UNAUTHORIZED_CLIENT,
                    ex.getMessage());
        }
        if (logEntity != null && !StringUtil.isEmpty(logEntity.getDisplayerror())) {
            return ResponseResult.error(TapHttpStatus.HTTP_INTERNAL_ERROR, TapHttpErrorCode.ERROR_FORMAT,
                    logEntity.getDisplayerror());
        }
        // 通用Exception异常处理
        return ResponseResult.error(TapHttpStatus.HTTP_INTERNAL_ERROR, TapHttpErrorCode.ERROR, ex.getMessage());
    }
    /**
     * @Description:    提示给用户的信息
     * @Author:         wuyuyang
     * @Return:         com.topscomm.pub.vo.ResponseResult<java.lang.Object>
     * @exception:
     * @Date:           28/6/2023 上午9:53
     * @Version:        1.0
     **/
    protected ResponseResult<Object> handleControllerMessage(String message) {
        // 通用Exception异常处理
        return ResponseResult.error(TapHttpStatus.HTTP_INTERNAL_ERROR, TapHttpErrorCode.ERROR_MESSAGE, message);
    }
}
